package cn.tedu.yushougeproduct.controller;

import cn.tedu.yushougeproduct.pojo.param.CategoryAddNewParam;
import cn.tedu.yushougeproduct.pojo.param.CategoryUpdateInfoParam;
import cn.tedu.yushougeproduct.pojo.vo.BrandListItemVO;
import cn.tedu.yushougeproduct.pojo.vo.CategoryListItemVO;
import cn.tedu.yushougeproduct.pojo.vo.CategoryStandardVO;
import cn.tedu.yushouge.commons.pojo.vo.PageData;
import cn.tedu.yushougeproduct.service.ICategoryService;
import cn.tedu.yushouge.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/category/")
@Slf4j
@Api(tags = "01：商品分类管理模块")
@Validated
public class CategoryController {

    @Autowired
    ICategoryService iCategoryService;


    @PostMapping("add-new")
    @ApiOperation("添加商品分类")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid CategoryAddNewParam categoryAddNewParam) {
        log.debug("开始处理【添加商品分类】的请求，参数：{}", categoryAddNewParam);
        iCategoryService.addNew(categoryAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("delete")
    @ApiOperation("根据ID删除商品分类")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品分类ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@Range(min = 1, message = "根据ID删除商品分类失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据ID删除商品分类】的请求，参数：{}", id);
        iCategoryService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("update")
    @ApiOperation("修改商品分类信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品分类ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@Range(min = 1, message = "请提交有效的商品分类ID值！") @RequestParam Long id,
                                     @Valid CategoryUpdateInfoParam categoryUpdateInfoParam) {
        log.debug("开始处理【修改商品分类详情】的请求，ID：{}，新数据：{}", id, categoryUpdateInfoParam);
        iCategoryService.updateInfoById(id, categoryUpdateInfoParam);
        return JsonResult.ok();
    }

    @PostMapping("enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setEnable(@Range(min = 1, message = "请提交有效的类别ID值！") @RequestParam Long id) {
        log.debug("开始处理【启用类别】的请求，参数：{}", id);
        iCategoryService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisable(@Range(min = 1, message = "请提交有效的类别ID值！") @RequestParam Long id) {
        log.debug("开始处理【禁用类别】的请求，参数：{}", id);
        iCategoryService.setDisable(id);
        return JsonResult.ok();
    }

    @PostMapping("display")
    @ApiOperation("显示类别")
    @ApiOperationSupport(order = 330)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisplay(@Range(min = 1, message = "请提交有效的类别ID值！") @RequestParam Long id) {
        log.debug("开始处理【显示类别】的请求，参数：{}", id);
        iCategoryService.setDisplay(id);
        return JsonResult.ok();
    }

    @PostMapping("hide")
    @ApiOperation("隐藏类别")
    @ApiOperationSupport(order = 340)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setHide(@Range(min = 1, message = "请提交有效的类别ID值！") @RequestParam Long id) {
        log.debug("开始处理【隐藏类别】的请求，参数：{}", id);
        iCategoryService.setHide(id);
        return JsonResult.ok();
    }

    @GetMapping("standard")
    @ApiOperation("根据ID查询商品分类详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品分类ID", required = true, dataType = "long")
    })
    public JsonResult<CategoryStandardVO> getStandardById(@Range(min = 1, message = "根据ID查询商品分类详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) throws Exception {
        log.debug("开始处理【根据ID查询商品分类详情】的请求，参数：{}", id);
        CategoryStandardVO result = iCategoryService.getStandardById(id);
        return JsonResult.ok(result);
    }

    @GetMapping("list")
    @ApiOperation("查询商品分类")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult<PageData<CategoryListItemVO>> list(@Range(min = 1, message = "查询商品分类失败，请提供正确的页码值！") Integer page,
                           String queryType) {
        log.debug("开始处理【查询商品分类列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<CategoryListItemVO> pageData;
        if ("all".equals(queryType)) {
            pageData = iCategoryService.list(pageNum, Integer.MAX_VALUE);
        } else {
            pageData = iCategoryService.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }
}
